Wang Haihua
🚅 🚋😜 🚑 🚔
Logical operators in python are and, or and not
t, f = True, False
print(type(t))
<class 'bool'>
Operator | Name |
---|---|
== | Equal |
!= | Not Equal |
< | Smaller Than |
> | Greater Than |
<= | Smaller or Equal to |
>= | Greater or Equal to |
print(t)
print(f)
print(t or f)
print(t and f)
print(not t) # not: True if operand is false (complements the operand)
print(t != f) # != not equal
print(t==f) # equal
True False True False False True False
This is the conversion of one data type to another. Important: This may result in a loss of data! (Why?)
my_int = 5
print(type(my_int))
my_int = str(my_int)
print(type(my_int))
<class 'int'> <class 'str'>
str("Python")
'Python'
float(5)
5.0
type(5.0)
float
Remember we mentioned loss of data? Look at the following:
int(5.7)
5
my_float = 5.7
my_float = int(my_float)
print(my_float)
5
float("6.2")
6.2
float("Hello")
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-10-ff6885467a56> in <module> ----> 1 float("Hello") ValueError: could not convert string to float: 'Hello'
int(5.5)
5
hello = "Hello"
print(hello)
print(hello[0])
print(hello[1])
print(hello[2])
print(hello[3])
print(hello[4])
Hello H e l l o
hello2 = " Hello"
hello2[0]
' '
hello
'Hello'
Don't forget that indexing starts with 0:
print(len(hello))
print(hello[5])
5
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-15-9edea1b9d9a4> in <module> 1 print(len(hello)) ----> 2 print(hello[5]) IndexError: string index out of range
BUT, when we we want to start from the end, we start with -1:
world = "World"
print(world)
print(world[-1])
print(world[-3])
print(world[-5])
World d r W
world[-6]
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-17-957f0f60f0cb> in <module> ----> 1 world[-6] IndexError: string index out of range
job = " Engineering"
print(job[-5])
e
You can slice the string by defining which index you want to start at and which one you want to end at (excluding the ending index)
For example, if we start at index 0 and end at index 2:
M | y | n | a | m | e | i | s | T | e | d | d | y | |||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
print(hello)
Hello
hello[0:2] # [x:y] --> take the values from x th to y th but don't take y th value.
'He'
world
'World'
world[1:4]
'orl'
print(world[3:5])
print(world[3:])
ld ld
hello[:4]
'Hell'
hello[:]
'Hello'
hello[:-1]
'Hell'
A third parameter can be added representing stride or step.
For example [0:4:2] means start at index 0 and end at 4 by stepping 2 indexes at a time
hello[::2]
'Hlo'
len(world)
5
hello
'Hello'
hello[2:len(world)]
'llo'
world[2:4:1] # [start:end:step]
'rl'
city = "istanbul"
city[0:6:2]
'itn'
Operator | Name |
---|---|
in | Returns True if value is present |
not in | Returns True if value is not present |
"t" in city
True
"y" in city
False
"anb" in city
True
"snl" in city
False
.capitalize() Capitalizes the first character
my_string = 'data science'
print(my_string.capitalize())
Data science
.upper() makes all characters upper case
print(my_string.upper())
print(my_string)
DATA SCIENCE data science
.replace(parameter1, parameter2) replaces parameter1 with parameter2 (if it finds parameter1)
print(my_string.replace('data','computer'))
computer science
my_string
'data science'
.strip() trims the empty spaces at the beginning and end of the string
word2 = " Trim this string "
print(word2.strip())
Trim this string
y = input("Please enter a city name: ") #input method always takes string type.
print("You entered: ", y)
Please enter a city name: You entered:
type(y)
str
x = int(input("Please enter an integer: "))
print(x)
Please enter an integer: 2 2
type(x)
int
Create a program will compute the tax and the tip for a meal ordered at a restaurant. You can compute the tax as 8 percent of the meal amount and the tip as 10 percent of the meal amount (without the tax). The output from your program should include the tax amount, the tip amount, and the grand total for the meal including both the tax and the tip.
a)Define the cost of the meal in the beginning of your program.
Some several example program runs:
Cost of the meal is 25 Eur.
Sample Run: The tax is 2.00 Eur and the tip is 2.50 Eur, making the total 29.50 Eur.
Cost of the meal is 68 Eur.
Sample Run: The tax is 5.44 Eur and the tip is 6.80000000000001 Eur, making the total 80.24 Eur.
b)Input the cost of the meal from the user.
Some several example program runs:
Please enter the cost of your meal: 100
Sample Run: The tax is 8.00 Eur and the tip is 10.00 Eur, making the total 118.00 Eur.
Please enter the cost of your meal: 68
Sample Run: The tax is 5.44 Eur and the tip is 6.80 Eur, making the total 80.24 Eur.
# a)Define the cost of the meal in the beginning of your program.
cost = 79
tax = cost * 0.08
tip = cost * 0.1
total = cost + tax + tip
print("The tax is " + str(tax) + " Eur and the tip is " + str(tip) + " Eur, making the total " + format(total, ".2f") + " Eur.")
print("The tax is " + format(tax, ".2f") + " Eur and the tip is " + format(tip, ".2f") + " Eur, making the total " + format(total, ".2f") + " Eur")
The tax is 6.32 Eur and the tip is 7.9 Eur, making the total 93.22 Eur. The tax is 6.32 Eur and the tip is 7.90 Eur, making the total 93.22 Eur
#b)Input the cost of the meal from the user.
cost = float(input("Please enter the cost of your meal: "))
tax = cost * 0.08
tip = cost * 0.1
total = cost + tax + tip
print("The tax is " + str(tax) + " Eur and the tip is " + str(tip) + " Eur, making the total " + format(total, ".2f") + " Eur")
Please enter the cost of your meal: 123@456.com
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-48-a9bf6a394f15> in <module> 1 #b)Input the cost of the meal from the user. 2 ----> 3 cost = float(input("Please enter the cost of your meal: ")) 4 tax = cost * 0.08 5 tip = cost * 0.1 ValueError: could not convert string to float: '123@456.com'
# creating a list
mylist = [3,5,6,7]
print(mylist)
[3, 5, 6, 7]
type(mylist)
list
print(mylist[0])
print(mylist[2])
print(mylist[-1])
print(mylist[-3])
3 6 7 5
mylist[2] = "python" # lists can be taken different types of data
print(mylist)
[3, 5, 'python', 7]
mylist.append('course') # append(): adding some items end of the list
print(mylist)
[3, 5, 'python', 7, 'course']
mylist = [3,4,5,6,7]
mylist.append('course')
mylist.append('course')
print(mylist)
[3, 4, 5, 6, 7, 'course', 'course']
Removes the last element from the specified position
popped = mylist.pop() # pop(): removing the last item of the list
print(popped)
print(mylist)
course [3, 4, 5, 6, 7, 'course']
mylist
[3, 4, 5, 6, 7, 'course']
Returns the index of the first element with the specifies value
mylist.index("course")
5
mylist.index(4)
1
Returns the number of elements with the specified value
mylist.count("course")
1
list2 = ["Python","Java","R","JavaScript","Ruby","Python","Python"]
list2.count("Python")
3
mylist
[3, 4, 5, 6, 7, 'course']
Removes the element with the specified value
mylist.remove("course")
print(mylist)
[3, 4, 5, 6, 7]
mylist.remove()
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-63-0fdc753c494d> in <module> ----> 1 mylist.remove() TypeError: remove() takes exactly one argument (0 given)
mylist.remove("course")
mylist.remove("python")
print(mylist)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-64-2afa78f0dea0> in <module> ----> 1 mylist.remove("course") 2 mylist.remove("python") 3 print(mylist) ValueError: list.remove(x): x not in list
Sorts the list
mylist.sort()
mylist
[3, 4, 5, 6, 7]
list3 = [100,23,87,13,1000]
list3.sort()
list3
[13, 23, 87, 100, 1000]
list4 = [41,23,78,99,37,2.9,2.8]
list4.sort()
list4
[2.8, 2.9, 23, 37, 41, 78, 99]
list5 = [41,23,78,99,37,'python']
list5.sort()
list5
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-69-05f8850dd456> in <module> 1 list5 = [41,23,78,99,37,'python'] ----> 2 list5.sort() 3 list5 TypeError: '<' not supported between instances of 'str' and 'int'
mylist.sort()
mylist
[3, 4, 5, 6, 7]
mylist2 = ["python", "course", "hello"]
mylist2.sort()
mylist2
['course', 'hello', 'python']
Reverses the order of the list
mylist = [3,4,5,6,7]
mylist.reverse()
print(mylist)
[7, 6, 5, 4, 3]
Add the elements of the list to te end of the current list
mylist3 = [1, 11, 111, 1111]
mylist.extend(mylist3)
mylist
[7, 6, 5, 4, 3, 1, 11, 111, 1111]
mylist4 = [1, 11, 111, 1111]
mylist.append(mylist4)
print(mylist)
[7, 6, 5, 4, 3, 1, 11, 111, 1111, [1, 11, 111, 1111]]
list_in_list = ["python","Java",3.2, 4, 11, [5,65,7,8,9]]
print(list_in_list[5])
[5, 65, 7, 8, 9]
list_in_list[-1]
[5, 65, 7, 8, 9]
Adds an element at the specified position
mylist = [2, 3, 4, 5, 6, 'python', 'flutter', 'Android', 'JavaScript', 'dart', 3.2, 5.0]
print(mylist)
mylist.insert(5,55) # insert(x,y) --> adding x th index the y value but don't change the x th value, it remains the same.
print(mylist)
[2, 3, 4, 5, 6, 'python', 'flutter', 'Android', 'JavaScript', 'dart', 3.2, 5.0] [2, 3, 4, 5, 6, 55, 'python', 'flutter', 'Android', 'JavaScript', 'dart', 3.2, 5.0]
a = list([1,2,3,4,5,6])
a
[1, 2, 3, 4, 5, 6]
list1 = list()
numbers = list(range(8))
print(numbers)
print(list1)
[0, 1, 2, 3, 4, 5, 6, 7] []
numbers2 = list(range(2,15,3)) #range(start, stop, step)
print(numbers2)
numbers2.reverse()
print(numbers2)
[2, 5, 8, 11, 14] [14, 11, 8, 5, 2]
numbers
[0, 1, 2, 3, 4, 5, 6, 7]
print(numbers[2:5])
[2, 3, 4]
numbers[5:8] = [10,11,12]
numbers
[0, 1, 2, 3, 4, 10, 11, 12]
12 in numbers
True
15 in numbers
False
[1,2,3] + [ 4,5]
[1, 2, 3, 4, 5]
[1,2,3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
course = "python"
s = input("Please enter a character: ")
if s in course:
print("Exist!")
else:
print("Don't exist...")
Please enter a character: Exist!
course = "python"
s = input("Please enter a character: ")
if s not in course:
print("Don't exist...")
else:
print("Exist!")
Please enter a character: w Don't exist...